package com.system.orders.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.common.bean.Payment;
import com.system.common.exception.ServiceException;
import com.system.common.service.IPaymentService;
import com.system.orders.mapper.PaymentMapper;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements IPaymentService {

    @Override
    public Payment getPaymentById(Integer id) {
        return super.getById(id);
    }

    @Override
    public Payment getPaymentByOrderNo(String orderno) {
        LambdaQueryWrapper<Payment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Payment::getOrderno, orderno);
        return super.getOne(queryWrapper);
    }

    @Override
    public void addPayment(Payment payment) {
        super.save(payment);
    }

    @Override
    public void updatePayment(Payment payment) {
        super.updateById(payment);
    }

    @Override
    public void deletePayment(Integer id) {
        Payment payment = super.getById(id);
        if (payment == null) {
            throw new ServiceException("支付记录不存在");
        }
        super.removeById(id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        Payment payment = super.getById(id);
        if (payment == null) {
            throw new ServiceException("支付记录不存在");
        }
        payment.setPaystatus(status);
        super.updateById(payment);
    }

    @Override
    public List<Payment> getPaymentList() {
        return super.list();
    }

    @Override
    public IPage<Payment> getPage(Payment payment, Page<Payment> page) {
        LambdaQueryWrapper<Payment> queryWrapper = new LambdaQueryWrapper<>();
        return baseMapper.selectPage(page, queryWrapper);
    }
}
